//2009/08/25 15:38:41
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <sstream>
#include <algorithm>

using namespace std;

class TheEquation
{
public:
    int leastSum(int X, int Y, int P)
    {
        int minSum = -1;
        for (int i=1; i<=1000; i++)
            for (int j=1; j<=1000; j++)
                if ((i*X+j*Y)%P ==0)
                    if (minSum > i+j || minSum == -1)
                        minSum = j+i;
        return minSum;
    }
};
